﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace proyecto
{
    public class Tab : DockPanel
    {

        AvalonDock.ResizingPanel[] subpanels=new AvalonDock.ResizingPanel[2];
        int NPanels;
        Vis[] vis;
        int nVis;
        AvalonDock.ResizingPanel panel;
        AvalonDock.DockingManager manager;
        ContentControl content;

        MemoryStream savedLayout = new MemoryStream();

        dockingTab docking;

        public Tab(){
           

            LastChildFill = true;

            NPanels = 0;

            vis = new Vis[6];
            nVis = 0;

            manager = new AvalonDock.DockingManager();
            
            manager.Loaded += new RoutedEventHandler(_dockingManager_Loaded);
            manager.HorizontalAlignment = HorizontalAlignment.Stretch;
            manager.VerticalAlignment = VerticalAlignment.Stretch;
            content = new ContentControl();
            
            
            
            content.HorizontalAlignment = HorizontalAlignment.Stretch;
            content.VerticalAlignment = VerticalAlignment.Stretch;
            panel = new AvalonDock.ResizingPanel();
            panel.Orientation = Orientation.Vertical;
            /*
            panel.HorizontalAlignment = HorizontalAlignment.Left;
            panel.VerticalAlignment = VerticalAlignment.Top;
            //panel.Height = 50;
            //panel.Width = 50;

            */
            
            subpanels=new AvalonDock.ResizingPanel[2];
            subpanels[0] = new AvalonDock.ResizingPanel();
            subpanels[0].Orientation = Orientation.Horizontal;
            subpanels[1] = new AvalonDock.ResizingPanel();
            subpanels[1].Orientation = Orientation.Horizontal;
            
            //panel.Children.Add(subpanels[0]);
            //panel.Children.Add(subpanels[1]);
            
            subpanels[0].Background = Brushes.Transparent;
            subpanels[1].Background = Brushes.Transparent;
            manager.Background = Brushes.Transparent;
            content.Background = Brushes.Transparent;
            panel.Background = Brushes.Transparent;


            docking = new dockingTab();
            
            manager.Content = subpanels[0];
            content.Content = manager;
            Children.Add(content);


            //Children.Add(subpanels[0]);
            //Children.Add(subpanels[1]);


            //Children.Add(panel);
                       
            
            //this.Children.Add(content);
            //MessageBox.Show("pasa");
        }


        public void AddVisualization(data matrix, int type, int table)
        {

            if (nVis == 6)
            {
                //throw new System.InvalidOperationException("Maximum number of visualizations reached");

                //return(0);
            }

            //ImageSourceConverter source = new ImageSourceConverter();

            //vis[nVis]= new Vis(source.ConvertFromString("http://rcgp.files.wordpress.com/2009/05/notas-musicales3.jpg") as ImageSource);



            /*if (nVis=0){
                DockPanel.SetDock(vis[nVis],Dock.Left);
                subpanels[0].Children.Add(vis[nVis]);
                DockPanel.SetDock(subpanels[NPanels],Dock.Top);
                Children.Add(subpanels[NPanels]);
                nVis++;
                NPanels++;

                return(1);
            }*/

            else
            {

                vis[nVis] = new Vis(matrix, type);
                
                if (vis[nVis].getCanceled())
                {
                    vis[nVis] = null;
                    GC.Collect();
                }

                else
                {


                    //vis[nVis] = new Vis(v);

                    //DockPanel.SetDock(vis[nVis], Dock.Left);
                    
                    //subpanels[0].Children.Add(vis[nVis]);

                    (manager.Content as AvalonDock.ResizingPanel).Children.Add(vis[nVis]);
                    nVis++;

                    //manager.Show(vis[nVis].aux);
                    

                   

                    //docking.topPanel.Children.Add(vis[nVis]);

                    if (nVis == 0)
                        //Children.Add(subpanels[1]);
                    //MessageBox.Show("paso");
                    //DockPanel.SetDock(subpanels[0], Dock.Top);

                    //DockPanel.SetDock(subpanels[1], Dock.Bottom);


                    //vis[0].ismou
                    /*int aux = subpanels[0].Children.Count - subpanels[1].Children.Count;
            

            

            
                

                            //int nChildren= subpanels[i].Children.Count

                            if (subpanels[aux].Children.Count == 0)
                            {
                        
                                DockPanel.SetDock(vis[nVis], Dock.Left);
                                subpanels[aux].Children.Add(vis[nVis]);
                                if (aux == 0)
                                {
                            
                                    DockPanel.SetDock(subpanels[aux], Dock.Top);
                                }
                                else { 
                                    DockPanel.SetDock(subpanels[aux], Dock.Bottom);
                                }

                                Children.Add(subpanels[aux]);

                        

                        

                                //SIN TERMINAR
                            }

                            else
                            {
                                if (subpanels[aux].Children.Count == 1)
                                    DockPanel.SetDock(vis[nVis], Dock.Right);

                                subpanels[aux].Children.Add(vis[nVis]);

                       

                   
                            }*/
                    nVis++;
                }

                } 
                

            }

        public void refresh(data matrix, int table)
        {
            for (int i = 0; i < nVis; i++)
                //if (table == vis[i].getTableNumber())
                    vis[i].refresh(matrix);
        }

        public void click(data matrix, MouseButtonEventArgs e)
        {
            for (int i = 0; i < nVis; i++)
                if (vis[i].IsMouseOver)
                    vis[i].click(matrix, e);
        }

        public void refreshCombos(data matrix)
        {
            
            for (int i = 0; i < nVis; i++)
                vis[i].refreshCombo(matrix);

        }

        public int getNVis()
        {
            return nVis;
        }

        public Vis getVis(int i)
        {
            return vis[i];
        }

        public void setNVis(int i)
        {
            nVis = i;
        }



        private void _dockingManager_Loaded(object sender, RoutedEventArgs e)
        {
            manager.SaveLayout(savedLayout);

            //_dockingManager.Hide(ShowMeSecond);
            //_dockingManager.Hide(AlsoShowMeSecond);

        }

        public void CloseVisualization(Vis v)
        {
            int i;
            for (i = 0; i < nVis && vis[i] != v; i++) { ;}


            (manager.Content as AvalonDock.ResizingPanel).Children.Remove(vis[i]);
            for (int j = nVis - 1; j > i; j--)
                vis[j - 1] = vis[j];

            

            nVis--;

            //MessageBox.Show(i.ToString());


        }

        
    }
}
